package easy;

public class Solution_1160 {

    public int countCharacters(String[] words, String chars) {
        int length = 0;
        int[] charCount = new int[26];
        char[] charArray = chars.toCharArray();
        for (char c : charArray) {
            charCount[c - 'a']++;
        }
        int[] arr = new int[26];
        for (String word : words) {
            boolean flag = true;
            char[] array = word.toCharArray();
            for (char c : array) {
                int v = c - 'a';
                arr[v]++;
                if (arr[v] > charCount[v]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                length += array.length;
            }
            for (int i = 0; i < arr.length; i++) {
                arr[i] = 0;
            }
        }
        return length;
    }

    public static void main(String[] args) {
        Solution_1160 model = new Solution_1160();
        System.out.println(model.countCharacters(new String[]{"cat","bt","hat","tree"}, "atach"));
        System.out.println(model.countCharacters(new String[]{"hello","world","leetcode"}, "welldonehoneyr"));
    }
}
